'use strict';
const crypto=require('crypto')
module.exports = app => {
    const { STRING, INTEGER, DATE, ENUM } = app.Sequelize;
    // 配置（重要：一定要配置详细，一定要！！！）
    const User = app.model.define('user', {
        id: {
            type: INTEGER(30).UNSIGNED,
            primaryKey: true,
            autoIncrement: true
        },
        username: {
            type: STRING(200),
            allowNull: false,
            defaultValue: '',
            comment: '用户名称',
            unique: true
        },
        password: {
            type: STRING(200),
            allowNull: false,
            defaultValue: '',
             set(val) {
                const hmac = crypto.createHash("sha256", app.config.crypto.secret);
                hmac.update(val);
                let hash = hmac.digest("hex");
                this.setDataValue('password', hash);
            }
        },
        avatar_url: { type: STRING(200), allowNull: true, defaultValue: '' },
        sex: { type: ENUM, values: ['男', '女', '保密'], allowNull: true, defaultValue: '男', comment: '用户性别' },
           status: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: '状态 0禁用 1启用 2离职'
      },
        created_at: {
            type: DATE,
            get() {
                const val = this.getDataValue('created_at');
                return (new Date(val)).getTime();
            }
        },
        updated_at: DATE
    });

    return User;
};